﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using CrystalDecisions.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using PM.Report;

namespace PatientManagement
{
    public partial class frmBaoCaoCuoiNgay : Form
    {
        // Methods
        public frmBaoCaoCuoiNgay()
        {
            this.InitializeComponent();
            this.connStr = Connection.ConnectionString();
        }
        private void btExit_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btXem_Click(object sender, EventArgs e)
        {
            if (this.cboBaoCao.Text == "")
            {
                MessageBox.Show("Vui lòng chọn mẫu báo cáo cần xem", "Mẫu Báo Cáo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.ddlNgayA.CustomFormat = "MM/dd/yyyy";
                this.ddlNgayB.CustomFormat = "MM/dd/yyyy";
                string str = this.cboBaoCao.SelectedItem.ToString();
                string str2 = string.Empty;
                if (this.cboLoaiBenh.Items.Count == 0)
                {
                    MessageBox.Show("Hiện tại chưa có loại bệnh để thống kê", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    switch (str)
                    {
                        case "Doanh Thu":
                            {
                                SqlConnection selectConnection = new SqlConnection(this.connStr);
                                if (!this.chkAll.Checked)
                                {
                                    this.sql = string.Concat(new object[] { "SELECT distinct * FROM vw_ClinicalInvoices WHERE empISN=", this.cbDoctorName.SelectedValue, " AND (clrExaminationDate  between '", this.ddlNgayA.Text, " 00:00:00 AM' and '", this.ddlNgayB.Text, " 00:00:00 AM') and ((invExaminationPayment IS not NULL ) and (invMedicinePayment IS not NULL ) and (invMedicineSkillPayment IS not null ) and (invSurgeryPayment IS not NULL ))" });
                                }
                                else
                                {
                                    this.sql = "SELECT distinct * FROM vw_ClinicalInvoices WHERE (clrExaminationDate  between '" + this.ddlNgayA.Text + " 00:00:00 AM' and '" + this.ddlNgayB.Text + " 00:00:00 AM') and ((invExaminationPayment IS not NULL ) and (invMedicinePayment IS not NULL ) and (invMedicineSkillPayment IS not null ) and (invSurgeryPayment IS not NULL ))";
                                }
                                SqlDataAdapter adapter = new SqlDataAdapter(this.sql, selectConnection);
                                DataSet dataSet = new DataSet();
                                this.kq = adapter.Fill(dataSet, "vw_ClinicalInvoices");
                                if (this.kq == 0)
                                {
                                    this.ddlNgayA.CustomFormat = "dd/MM/yyyy";
                                    this.ddlNgayB.CustomFormat = "dd/MM/yyyy";
                                    MessageBox.Show("Báo cáo hiện chưa có dữ liệu.Xin vui lòng chọn lại tháng hoặc tên bác sĩ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                    this.report.ReportSource = null;
                                }
                                else
                                {
                                    this.ddlNgayA.CustomFormat = "dd/MM/yyyy";
                                    this.ddlNgayB.CustomFormat = "dd/MM/yyyy";
                                    SubReport subreport = new SubReport();
                                    subreport.SetDataSource(dataSet);
                                    this.report.ReportSource = subreport;
                                }
                                break;
                            }
                        case "Chẩn Đoán":
                            {
                                SqlConnection connection2 = new SqlConnection(this.connStr);
                                if (this.chkAllSick.Checked && !this.chkAll.Checked)
                                {
                                    this.sql = string.Concat(new object[] { "SELECT distinct * FROM vw_PathologicalClinical WHERE empISN=", this.cbDoctorName.SelectedValue, " AND (clrExaminationDate  between '", this.ddlNgayA.Text, " 00:00:00 AM' and '", this.ddlNgayB.Text, " 00:00:00 AM') and pltName is not null" });
                                }
                                else if (this.chkAll.Checked && this.chkAllSick.Checked)
                                {
                                    this.sql = "SELECT distinct * FROM vw_PathologicalClinical WHERE (clrExaminationDate  between '" + this.ddlNgayA.Text + " 00:00:00 AM' and '" + this.ddlNgayB.Text + " 00:00:00 AM') and pltName is not null";
                                }
                                else if (!this.chkAll.Checked && !this.chkAllSick.Checked)
                                {
                                    str2 = this.cboLoaiBenh.SelectedValue.ToString();
                                    this.sql = string.Concat(new object[] { "SELECT distinct * FROM vw_PathologicalClinical WHERE empISN=", this.cbDoctorName.SelectedValue, " AND (clrExaminationDate  between '", this.ddlNgayA.Text, " 00:00:00 AM' and '", this.ddlNgayB.Text, " 00:00:00 AM') and pltISN = ", str2 });
                                }
                                else
                                {
                                    str2 = this.cboLoaiBenh.SelectedValue.ToString();
                                    this.sql = "SELECT distinct * FROM vw_PathologicalClinical WHERE (clrExaminationDate  between '" + this.ddlNgayA.Text + " 00:00:00 AM' and '" + this.ddlNgayB.Text + " 00:00:00 AM') and pltISN = " + str2;
                                }
                                SqlDataAdapter adapter2 = new SqlDataAdapter(this.sql, connection2);
                                DataSet set2 = new DataSet();
                                this.kq = adapter2.Fill(set2, "vw_PathologicalClinical");
                                if (this.kq == 0)
                                {
                                    this.ddlNgayA.CustomFormat = "dd/MM/yyyy";
                                    this.ddlNgayB.CustomFormat = "dd/MM/yyyy";
                                    MessageBox.Show("Báo cáo hiện chưa có dữ liệu.Xin vui lòng chọn lại tháng hoặc tên bác sĩ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                    this.report.ReportSource= null;
                                }
                                else
                                {
                                    this.ddlNgayA.CustomFormat = "dd/MM/yyyy";
                                    this.ddlNgayB.CustomFormat = "dd/MM/yyyy";
                                    ReportChanDoan doan = new ReportChanDoan();
                                    doan.SetDataSource(set2);
                                    this.report.ReportSource = doan;
                                }
                                break;
                            }
                        case "Chẩn Đoán Ngoài Danh sách":
                            {
                                SqlConnection connection3 = new SqlConnection(this.connStr);
                                if (!this.chkAll.Checked)
                                {
                                    this.sql = string.Concat(new object[] { "SELECT distinct * FROM vw_PathologicalClinical WHERE empISN=", this.cbDoctorName.SelectedValue, " AND (clrExaminationDate  between '", this.ddlNgayA.Text, " 00:00:00 AM' and '", this.ddlNgayB.Text, " 00:00:00 AM') and polPathologicalSignsOutList is not null and polPathologicalSignsOutList <> ''" });
                                }
                                else
                                {
                                    this.sql = "SELECT distinct * FROM vw_PathologicalClinical WHERE (clrExaminationDate   between '" + this.ddlNgayA.Text + " 00:00:00 AM' and '" + this.ddlNgayB.Text + " 00:00:00 AM') and polPathologicalSignsOutList is not null and polPathologicalSignsOutList <> ''";
                                }
                                SqlDataAdapter adapter3 = new SqlDataAdapter(this.sql, connection3);
                                DataSet set3 = new DataSet();
                                this.kq = adapter3.Fill(set3, "vw_PathologicalClinical");
                                if (this.kq == 0)
                                {
                                    this.ddlNgayA.CustomFormat = "dd/MM/yyyy";
                                    this.ddlNgayB.CustomFormat = "dd/MM/yyyy";
                                    MessageBox.Show("Báo cáo hiện chưa có dữ liệu.Xin vui lòng chọn lại tháng hoặc tên bác sĩ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                    this.report.ReportSource =null ;
                                }
                                else
                                {
                                    this.ddlNgayA.CustomFormat = "dd/MM/yyyy";
                                    this.ddlNgayB.CustomFormat = "dd/MM/yyyy";
                                    ReportChanDoanNgoaiDS ids = new ReportChanDoanNgoaiDS();
                                    ids.SetDataSource(set3);
                                    this.report.ReportSource = ids;
                                }
                                return;
                            }
                    }
                }
            }
        }

        private void cboBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((this.cboBaoCao.SelectedItem.ToString() == "Chẩn Đoán Ngoài Danh sách") || (this.cboBaoCao.SelectedItem.ToString() == "Doanh Thu"))
            {
                this.cboLoaiBenh.Enabled = false;
                this.chkAllSick.Enabled = false;
            }
            else
            {
                this.cboLoaiBenh.Enabled = true;
                this.chkAllSick.Enabled = true;
                this.chkAllSick.Checked = false;
            }
        }

        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkAll.Checked)
            {
                this.cbDoctorName.Enabled = false;
            }
            else
            {
                this.cbDoctorName.Enabled = true;
            }
        }

        private void chkAllSick_CheckedChanged(object sender, EventArgs e)
        {
            if (!this.chkAllSick.Checked)
            {
                this.cboLoaiBenh.Enabled = true;
            }
            else
            {
                this.cboLoaiBenh.Enabled = false;
            }
        }

        private void frmBaoCaoCuoiNgay_Load(object sender, EventArgs e)
        {
            SqlConnection selectConnection = new SqlConnection(this.connStr);
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Employees", selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "Employees");
            this.cbDoctorName.DisplayMember = "Employees.empName";
            this.cbDoctorName.ValueMember = "Employees.empISN";
            this.cbDoctorName.DataSource = dataSet;
            this.cboBaoCao.Items.Add("Doanh Thu");
            this.cboBaoCao.Items.Add("Chẩn Đoán");
            this.cboBaoCao.Items.Add("Chẩn Đoán Ngoài Danh sách");
            this.sql = "SELECT * from PathologicalTree";
            SqlDataAdapter adapter2 = new SqlDataAdapter(this.sql, selectConnection);
            DataSet set2 = new DataSet();
            adapter2.Fill(set2, "PathologicalTree");
            this.cboLoaiBenh.DataSource = set2;
            this.cboLoaiBenh.DisplayMember = "PathologicalTree.pltName";
            this.cboLoaiBenh.ValueMember = "PathologicalTree.pltISN";
        }
        private void Report_HandleException(object source, ExceptionEventArgs e)
        {
            if (e.Exception is EngineException)
            {
                EngineException exception = (EngineException)e.Exception;
                if (exception.ErrorID == EngineExceptionErrorID.DataSourceError)
                {
                    e.Handled = true ;
                    MessageBox.Show("An error has occurred while connecting to the database.");
                }
                else if (exception.ErrorID == EngineExceptionErrorID.LogOnFailed)
                {
                    e.Handled = true;
                    MessageBox.Show("Incorrect Logon Parameters. Check your user name and password.");
                }
            }
            if (e.Exception is ExecutionEngineException)
            {
                e.Handled = true;
                MessageBox.Show("ExecutionEngineException");
            }
        }

    }
}